'''
Created on May 25, 2011
rm -rf build dist
python setup.py py2app

@author: bpatel
'''
import telnetlib
import sys
import tkMessageBox
import time

DEBUG=False
KEY_UP=315
KEY_LEFT=314
KEY_RIGHT=316
KEY_DOWN=317
KEY_ENTER=13
KEY_ESCAPE=27
KEY_SPACE=32
KEY_BACKSPACE=8
KEY_I=73
KEY_F=70
KEY_R=82
class Events:
    '''
    Handle Events Here
    '''


    def __init__(self, host, port):
        '''
        Constructor
        '''
        self.host = host
        self.port = port
        
    def handle_back(self, event):
        self.send_command("press k")
        event.Skip()
        
    def handle_home(self, event):
        self.send_command("press home")
        event.Skip()
        
    def handle_up(self, event):
        self.send_command("press up")
        event.Skip()
        
    def handle_left(self, event):
        self.send_command("press left")
        event.Skip()
        
    def handle_right(self, event):
        self.send_command("press right")
        event.Skip()
        
    def handle_ok(self, event):
        self.send_command("press select")
        event.Skip()
        
    def handle_down(self, event):
        self.send_command("press down")
        event.Skip()
        
    def handle_rewind(self, event):
        self.send_command("press back")
        event.Skip()
        
    def handle_play(self, event):
        self.send_command("press play")
        event.Skip()
        
    def handle_forward(self, event):
        self.send_command("press forward")
        event.Skip()
        
    def handle_info(self, event):
        self.send_command("press info")
        event.Skip()
        
    def handle_unknown_command(self, event):
        if (DEBUG):
            keycode = event.GetKeyCode()
            print keycode
            print "Entered\n"
        event.Skip()
    def reboot_roku(self, event):
        self.send_command("press home")
        self.send_command("press home")
        self.send_command("press home")
        self.send_command("press home")
        self.send_command("press home")
        self.send_command("press up")
        self.send_command("press back")
        self.send_command("press back")
        self.send_command("press fwd")
        self.send_command("press fwd")
        event.Skip()
        
    def OnKeyDown(self, event):
        keycode = event.GetKeyCode()
        
        if (keycode == KEY_UP):
            self.handle_up(event)
            
        elif keycode == KEY_DOWN:
            self.handle_down(event)
            
        elif keycode == KEY_LEFT:
            self.handle_left(event)
                
        elif keycode == KEY_RIGHT:
            self.handle_right(event)
            
        elif keycode == KEY_ESCAPE:
            self.handle_back(event)
            
        elif keycode == KEY_I:
            self.handle_info(event)
            
        elif keycode == KEY_BACKSPACE:
            self.handle_home(event)
            
        elif keycode == KEY_SPACE:
            self.handle_play(event)
            
        elif keycode == KEY_ENTER:
            self.handle_ok(event)
        
        elif keycode == KEY_F:
            self.handle_forward(event)
        
        elif keycode == KEY_R:
            self.handle_rewind(event)
            
        else:
            self.handle_unknown_command(event) 
            #event.Skip()
    def setIPAddress(self, ip):
        self.host = ip
    def send_command(self, command):
        try:
            tn = telnetlib.Telnet(self.host, self.port)
            if tn:
                tn.write(command + "\n")
                tn.close()   
                time.sleep(1/5)    
        except:
            tkMessageBox.showwarning(
                "Connection Failed",
                "Unable to connect to Roku"
            )
            print sys.exc_info()
            print "Exception lost connection"
